#!/bin/bash
#################################################################
# @Description: 用例名称：日志处理与分析-IP修改监控回溯，测试目的：验证IP被非预期修改的监控能力
# @Date : 2023-11-22
# @Last Modified by :
# @Last Modified time:2023-11-25
#################################################################
# 获取待测试的网卡名称
read -p "输入测试网卡名(例如 enp4s0): " EthName

echo "测试前准备：安装监控工具软件包sysmonitor"
yum install -y sysmonitor net-tools  > /dev/null
systemctl start sysmonitor
# 重启 rsyslog 服务后，日志会存储到 /var/log/sysmonitor.log
systemctl restart rsyslog

echo "开始测试："
echo "1.检查sysmonitor和rsyslog服务状态是否为运行"
#确认服务启动正常，状态为“active (running)”
systemctl status sysmonitor | grep "active (running)" > /dev/null
if [ $? -ne 0 ]; then
  echo "sysmonitor 服务异常，请检查！ "
  exit 1
fi
# 确认rsyslog服务的状态
systemctl status rsyslog | grep "active (running)" > /dev/null
if [ $? -ne 0 ]; then
  echo "rsyslog 服务异常，请检查！ "
  exit 1
fi

echo "2.修改网口IP 地址为1.1.1.1，netmask 255.255.0.0"
ifconfig $EthName 1.1.1.1 netmask 255.255.0.0
echo "--------------------ip a--------------------"
ip a 
sleep 5

echo "3.查看网口修改日志"
grep "$EthName" /var/log/sysmonitor.log | grep "is added" | grep "1\.1\.1\.1" 
if [ $? -ne 0 ]; then
  echo -e "监控IP非预期修改\e[31m 测试未通过\e[0m"
else
  echo -e "监控IP非预期修改\e[32m 测试通过\e[0m"
fi


## 后处理
echo "测试完成进行环境清理"
ifconfig $EthName 0.0.0.0
yum remove -y sysmonitor  > /dev/null
systemctl restart NetworkManager
rm -rf /var/log/sysmonitor.log